package com.steven.im.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.steven.im.App;
import com.steven.im.R;
import com.steven.im.base.BaseActivity;
import com.steven.im.entity.ImBaseEntity;
import com.steven.im.net.MyCallback;
import com.steven.im.net.Task;
import com.steven.im.utils.DataCheckUtils;
import com.steven.im.utils.ScreenUtil;
import com.steven.im.utils.TitleBuilder;
import com.steven.im.utils.ToastUtil;
import com.steven.im.widget.CountTimer;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class IMChangePhoneActivity extends BaseActivity {
    private String smsKey = "";//发送短信返回的key
    CountTimer countTimer;
    @BindView(R.id.et_phone)//手机号
            EditText et_phone;
    @BindView(R.id.et_phoneCode)//手机验证码
            EditText et_phoneCode;
    @BindView(R.id.tv_getcode)//获取验证码
            TextView tvGetcode;
    @BindView(R.id.tv_login)
    TextView tv_login;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;

    @Override
    public void setListen() {
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (et_phone.getText().toString().length() > 0) {
                    tv_login.setBackgroundResource(R.drawable.bg_btn);
                } else {
                    if (("").equals(et_phoneCode.getText().toString()) && ("").equals(et_phoneCode.getText().toString())) {
                        tv_login.setBackgroundResource(R.drawable.bg_btn_gray);
                    }
                }
            }
        });
        et_phoneCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (et_phoneCode.getText().toString().length() == 0) {
                    if (("").equals(et_phoneCode.getText().toString()) && ("").equals(et_phoneCode.getText().toString())) {
                        tv_login.setBackgroundResource(R.drawable.bg_btn_gray);
                    }
                } else {
                    tv_login.setBackgroundResource(R.drawable.bg_btn);
                }
            }
        });
    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_im_changephone;
    }

    @Override
    public void initView() {
        ivTitleRight.setVisibility(View.GONE);
        new TitleBuilder(this).setTitleText("")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });

        countTimer = new CountTimer(tvGetcode);
    }

    @Override
    public void initData() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countTimer != null) {
            countTimer.cancel();
            tvGetcode.setEnabled(true);
            tvGetcode.setText("获取验证码");
        }
    }


    @OnClick({R.id.tv_getcode, R.id.tv_login})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_getcode://获取验证码
                if (!("").equals(et_phone.getText().toString()) && DataCheckUtils.isCellphone(et_phone.getText().toString())) {
                    sendVerify();
                } else {
                    ToastUtil.showShort(App.getContext(), "不合法的手机号！");
                }
                break;
            case R.id.tv_login://注册
                break;
        }
    }


    //用户登录

    /**
     * 获取验证码
     */
    private void sendVerify() {
        countTimer.start();
        Task.getApiService().sendImPhoneCode(et_phone.getText().toString()).enqueue(new MyCallback<ImBaseEntity>(IMChangePhoneActivity.this) {
            @Override
            protected void onSuccess(Response<ImBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    smsKey = response.body().getData().toString();
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                ToastUtil.showShort(App.getContext(), "发送失败！");
            }
        });
    }

}
